package com.boful.common.file.model;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;

import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;

class SMBFileSystem extends FileSystem {
	// smb://administrator:ligson@192.168.1.111

	private String url;
	private SmbFile smbFile;

	public SMBFileSystem(String url) throws Exception {
		if (url.startsWith(getScheme()) || url.startsWith("\\\\")) {
			String formatPath = normalize(url);
			System.out.println(formatPath);
			smbFile = new SmbFile(formatPath);
		} else {
			throw new Exception("unsupport scheme:" + url);
		}
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public char getSeparator() {
		return '/';
	}

	@Override
	public String normalize(String path) {
		if (path.startsWith("\\\\")) {
			String tmp = path.replaceAll("\\", getSeparator() + "");
			return getScheme() + ":" + tmp;
		} else {
			return path;
		}
	}

	@Override
	public String getDefaultParent() {
		return smbFile.getParent();
	}

	@Override
	public boolean isAbsolute() {
		return true;
	}

	@Override
	public long getLastModifiedTime() {

		return smbFile.getLastModified();
	}

	@Override
	public long getLength() {
		try {
			return smbFile.length();
		} catch (SmbException e) {
			e.printStackTrace();
			return -1;
		}
	}

	@Override
	public boolean delete() {
		try {
			smbFile.delete();
		} catch (SmbException e) {
			return false;
		}
		return true;
	}

	@Override
	public String[] list() {
		try {
			return smbFile.list();
		} catch (SmbException e) {
			return null;
		}
	}

	@Override
	public boolean createDirectory() {
		try {
			smbFile.mkdirs();
			return true;
		} catch (SmbException e) {
			return false;
		}

	}

	@Override
	public boolean rename(NetFile f2) {
		if (f2.getScheme().equals(getScheme())) {
			try {
				smbFile.renameTo(new SmbFile(f2.getUrl()));
				return true;
			} catch (Exception e) {
				return false;
			}
		}

		return false;
	}

	@Override
	public String getScheme() {
		return "smb";
	}

	@Override
	public InputStream getInputStream() {
		try {
			return smbFile.getInputStream();
		} catch (IOException e) {
			return null;
		}
	}

	@Override
	public OutputStream getOutputStream() {
		try {
			return smbFile.getOutputStream();
		} catch (IOException e) {
			return null;
		}
	}

	@Override
	public String getName() {
		return smbFile.getName();
	}

	@Override
	public boolean exist() {
		try {
			smbFile.exists();
			return true;
		} catch (SmbException e) {
			return false;
		}
	}

	@Override
	public boolean copyTo(NetFile destFile) {
		if (destFile.getScheme().equals(getScheme())) {
			try {
				smbFile.copyTo(new SmbFile(destFile.getUrl()));
				return true;
			} catch (SmbException | MalformedURLException e) {
				return false;
			}
		} else {
			return false;
		}

	}

	@Override
	public NetFile[] listFiles() {
		try {
			SmbFile[] smbFiles = smbFile.listFiles();
			NetFile[] netFiles = new NetFile[smbFiles.length];
			return netFiles;
		} catch (Exception e) {
			return null;
		}

	}

	@Override
	public String getContentType() {
		return smbFile.getContentType();
	}

	@Override
	public boolean isDirectory() {
		try {
			return smbFile.isDirectory();
		} catch (SmbException e) {
			return false;
		}
	}

}
